<?php

namespace app\admin\controller;

use app\admin\model\{Group as GroupModel, Rule as RuleModel};
use app\admin\validate\Group as GroupV;
use think\facade\View;

class Group extends Base
{
    /**
     * 角色列表
     */
    public function index()
    {
        $list = GroupModel::order('id asc')->paginate([
            'list_rows'     =>  20,
            'query'         =>  $this->get
        ]);
        return View::fetch('', ['list' => $list]);
    }

    /**
     * 添加角色
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            try {
                validate(GroupV::class)->check($this->post);
                (new GroupModel)->replace()->save($this->post);
            } catch (\Exception $e) {
                return $this->error($e->getMessage() ?: '添加失败');
            }
            return $this->success('添加成功', 'index');
        }
        //查询节点列表
        $where = [
            ['status', '=', 1],
        ];
        $rule_list = RuleModel::field('id,pid parent,name text')->where($where)->order('sort desc,id asc')->select()->toArray();
        foreach ($rule_list as $k => $v) {
            $rule_list[$k]['parent'] = $v['parent'] ?: '#';
        }
        return View::fetch('', ['rule_list' => $rule_list]);
    }

    /**
     * 修改角色
     */
    public function edit()
    {
        if ($this->get['id'] == 1) {
            return $this->error('超级管理员角色不可修改');
        }
        $where = [
            'id'    =>  $this->param['id'],
        ];
        $info = GroupModel::where($where)->find();
        if ($this->request->isAjax()) {
            try {
                validate(GroupV::class)->scene('edit')->check($this->post);
                $info->replace()->save($this->post);
            } catch (\Exception $e) {
                return $this->error($e->getMessage() ?: '修改失败');
            }
            return $this->success('修改成功', 'index');
        }
        //查询节点列表
        $where = [
            ['status', '=', 1],
        ];
        $rule_list = RuleModel::field('id,pid parent,name text')->where($where)->order('sort desc,id asc')->select()->toArray();

        foreach ($rule_list as $k => $v) {
            $rule_list[$k]['parent'] = $v['parent'] ?: '#';
            if (!RuleModel::where(['pid' => $v['id']])->find() && in_array($v['id'], explode(',', $info['rules']))) {
                $rule_list[$k]['state'] = ['selected' => true];
            } else {
                $rule_list[$k]['state'] = ['selected' => false];
            }
        }
        return View::fetch('', ['info' => $info, 'rule_list' => $rule_list]);
    }

    /**
     * 删除用户组
     */
    public function del()
    {
        if ($this->request->isAjax()) {
            try {
                if (!$info = GroupModel::where($this->post)->find())
                    throw new \Exception('用户组不存在');
                $info->delete();
            } catch (\Exception $e) {
                return $this->error($e->getMessage() ?: '删除失败');
            }
            return $this->success('删除成功');
        }
    }
}
